<template>
  <el-dialog title="结算确认" width="50%" :visible.sync="visible">
    <el-table
      :data="rows"
      height="250"
      border
      style="width: 100%;margin-bottom:20px"
    >
      <el-table-column prop="no" align="center" label="单号" width="180" />
      <el-table-column prop="supplierName" align="center" label="标记" width="70">
        <template slot-scope="scope">
          <column-filter v-model="scope.row.flag" type="supOrderFlag" />
        </template>
      </el-table-column>
      <el-table-column prop="settleQuantity" align="right" label="结算量(吨)" sortable width="120" />
      <el-table-column prop="deduct" align="right" label="扣点率(%)" sortable width="120" />
      <el-table-column prop="univalence" align="right" label="单价/吨" width="120" />
      <el-table-column prop="paymentAmount" align="right" label="结算金额" sortable width="150" />
      <el-table-column prop="createTime" sortable align="center" label="创建时间" />
    </el-table>
    <el-form v-if="form.params.supplierId" ref="form" :model="form" class="dialog-form" label-position="right" label-width="120px">
      <el-form-item label="支付类型" prop="params.bankId" :rules="$ruleStore.rules.def">
        <supplier-bank v-model="form.params.bankId" :supplier-id="form.params.supplierId" @change="bankChange" />
      </el-form-item>
      <div v-if="form.params.bank">
        <el-form-item v-if="form.params.bank.openingBank" label="开户行">
          <label>{{ form.params.bank.openingBank }}</label>
        </el-form-item>
        <el-form-item label="姓名">
          <label>{{ form.params.bank.accountName }}</label>
        </el-form-item>
        <el-form-item label="卡号">
          <label>{{ form.params.bank.cardNumber }}</label>
        </el-form-item>
      </div>
      <el-form-item label="核算金额">
        <label>￥<span class="money">{{ form.params.totalPaymentAmount.toFixed(2) }}</span></label>
        <!-- <el-tag v-if="flag>2" size="mini" effect="plain" type="danger">{{ $constants.supOrderFlag.get(flag).label }}</el-tag> -->
      </el-form-item>
      <!-- <el-form-item label="实付金额" prop="params.paymentAmount" :rules="parent.page.rules.num">
        <el-input-number v-model="form.params.paymentAmount" controls-position="right" :precision="2" :min="1" />
      </el-form-item> -->
      <el-form-item label="付款凭证" prop="params.certificate" :rules="$ruleStore.rules.def">
        <form-upload-image v-model="form.params.certificate" size="mini" :remote="upload.img" />
      </el-form-item>
      <el-form-item label="补充描述" prop="message">
        <el-input v-model="form.message" type="textarea" :rows="2" placeholder="请输入补充描述" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取 消</el-button>
      <el-button size="mini" type="primary" @click="submit">提交</el-button>
    </div>
  </el-dialog>
</template>
<script>
import SupplierBank from '@/components/SupplierBank'

export default {
  name: 'PurPaying',
  components: {
    SupplierBank
  },
  props: {
    value: {
      type: String,
      default: ''
    },
    parent: {
      type: Object,
      default: () => { return {} }
    }
  },
  data() {
    return {
      visible: false,
      form: {
        id: null,
        message: null,
        params: {
          certificate: '',
          bank: null,
          actualAmounts: [],
          paymentAmounts: [],
          bankId: null,
          suplierId: null,
          totalPaymentAmount: 0,
          ids: []
        }
      },
      rows: [],
      upload: {
        img: {
          url: this.$http.conf.api.get('portal').url + '/api/upload/img',
          path: 'upload/img',
          accept: 'image/*'
        }
      }
    }
  },
  created() {

  },
  methods: {
    show(supplierId, totalPaymentAmount, rows, actualAmount) {
      this.visible = true
      this.form.params.supplierId = supplierId
      this.form.params.totalPaymentAmount = totalPaymentAmount
      this.rows = rows
      var ids = []
      var actualAmounts = []
      var paymentAmounts = []
      rows.forEach(o => {
        ids.push(o.id)
        actualAmounts.push(o.paymentAmount)
        paymentAmounts.push(o.paymentAmount)
      })
      this.form.params.ids = ids
      this.form.params.actualAmounts = actualAmounts
      this.form.params.paymentAmounts = paymentAmounts
      this.form.params.certificate = ''
      this.form.params.bankId = null
    },
    bankChange(current) {
      this.form.params.bank = current
    },
    submit() {
      const that = this
      that.form.id = that.value
      that.$refs['form'].validate((valid) => {
        if (!valid) {
          return
        }
        if (that.form.params.bankId === null) {
          that.$message.error('请选中支付类型')
          return
        }
        const loading = that.$loading()
        const url = that.$http.conf.api.get('wms').url + '/api/pur/order-info//batch/paying'

        that.$http.post(url, that.form).then(res => {
          loading.close()
          if (res.code !== 200) {
            that.$message.error(res.message)
            return
          }
          that.$message.success('操作成功!')
          that.parent.search()
          that.visible = false
        }).catch((e) => {
          loading.close()
        })
      })
    }
  }
}
</script>
